package com.zhl.study.controller;

import com.zhl.study.service.IWeatherService;
import com.zhl.study.base.ResponseFactory;
import com.zhl.study.base.Result;
import com.zhl.study.entity.Weather;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author zl
 * @date 2023/8/31 10:11
 */
@RestController
@RequestMapping("/api")
@Api(value = "天气预报", tags = "天气预报")
public class WeatherController {

    @Autowired
    private IWeatherService weatherService;

    @RequestMapping("/weather/{cityCode}")
    @ApiOperation(httpMethod = "GET", value = "获取天气预报数据")
    public Result<List<Weather>> getWeather(@PathVariable String cityCode){
        //获取天气数据
        List<Weather> weathers = null;
        try {
            weathers = weatherService.getWeather(cityCode);
        }catch (IOException e){
            return ResponseFactory.fail();
        }
        return ResponseFactory.ok(weathers);
    }

}
